<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="订单编号">
              <a-input v-model="queryParam.transactionId" placeholder="请输入"/>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="商圈">
              <a-select allowClear v-model="queryParam.companyId">
                <a-select-option v-for="item in companys" :key="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="订单状态">
              <a-select v-model="queryParam.orderStatus"
                        allowClear
              >
                <a-select-option v-for="item in OrderStatus" :key="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <a-form-item label="起始时间">
              <a-date-picker
                format="YYYY-MM-DD"
                v-model="queryParam.startTime"
                placeholder="开始"
              />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="结束时间">
              <a-date-picker
                format="YYYY-MM-DD"
                placeholder="结束"
                v-model="queryParam.enTime"
              />
            </a-form-item>
          </a-col>

          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <span>总单数：<a-tag color="purple">{{statistics.sumCount}} 单</a-tag></span>
      <span>总金额：<a-tag color="purple">{{statistics.sumFee}} 元</a-tag></span>
      <span>退款单数：<a-tag color="red">{{statistics.refundCount}} 单</a-tag></span>
      <span>退款金额：<a-tag color="red">{{statistics.refundFee}} 元</a-tag></span>
    </div>
    <!--<div class="table-operator">
      <span>微信单数：<a-tag color="green">{{statistics.wxCount}} 单</a-tag></span>
      <span>微信金额：<a-tag color="green">{{statistics.wxFee}} 元</a-tag></span>
      <span>支付宝单数：<a-tag color="blue">{{statistics.aliCount}} 单</a-tag></span>
      <span>支付宝金额：<a-tag color="blue">{{statistics.aliFee}} 元</a-tag></span>
    </div>-->

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true, clear: true }"
      :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
      :scroll="{ x: 1500 }"
    >
      <p slot="expandedRowRender" slot-scope="record" style="margin: 0">订单地址：<b>{{record.address}}</b></p>
      <template slot-scope="text" slot="orderStatus">
        <a-tag v-if="text==1" color="blue">
          {{ text | statusFilter }}
        </a-tag>
        <a-tag v-if="text== 3" color="red">
          {{ text | statusFilter }}
        </a-tag>
      </template>
      <span slot="orderTotalFee" slot-scope="text">
        {{ text/100 }}元
      </span>
      <span slot="realPayFee" slot-scope="text">
        {{ text/100 }}元
      </span>
      <span slot="action" slot-scope="text, record">
        <template v-if="record.orderStatus==1">
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
         <a-divider v-if="record.orderStatus==1" type="vertical"/>
          <a @click="refund(record)">退款</a>
        </template>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>

  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import Edit from './Edit'
  import { selectList,refund} from '@/api/orderService/order'
  import { selectAll } from '../../../api/companyService/company'
  import moment from 'moment'

  export default {
    components: {
      STable,
      Edit
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        companys:[],
        OrderStatus: [{id: 1, name: '已支付'}, {id: 3, name: '已退款'}], //订单状态
        statistics: {
          sumFee: 0,
          sumCount: 0,
          aliCount: 0,
          aliFee: 0,
          wxCount: 0,
          wxFee: 0,
          refundCount: 0,
          refundFee: 0
        },
        // 表头
        columns: [
          {
            title: '订单编号',
            dataIndex: 'transactionId'
          },
          {
            title: '商品名称',
            dataIndex: 'productName'
          },
          {
            title: '创建时间',
            dataIndex: 'createdTime'
          },
          {
            title: '完成时间',
            dataIndex: 'updatedTime'
          },
          {
            title: '订单金额',
            dataIndex: 'orderTotalFee',
            scopedSlots: { customRender: 'orderTotalFee' }
          },
          {
            title: '实际支付金额',
            dataIndex: 'realPayFee',
            scopedSlots: { customRender: 'realPayFee' }
          },
          {
            title: '实际支付金额',
            dataIndex: 'orderStatus',
            scopedSlots: { customRender: 'orderStatus' }
          },
          {
            title: '物流公司名称',
            dataIndex: 'expressName'
          },
          {
            title: '物流单号',
            dataIndex: 'expressNo'
          },
          {
            title: '操作',
            width: '150px',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          const beginTimeQuery = this.queryParam.startTime //undefined
          const endTimeQuery = this.queryParam.enTime //undefined
          if (beginTimeQuery != undefined) {//点击查询

            this.queryParam.beginTime = moment(beginTimeQuery).format('YYYY-MM-DD') + ' 00:00:00'
          }
          if (beginTimeQuery != undefined) {
            this.queryParam.endTime = moment(endTimeQuery).format('YYYY-MM-DD') + ' 23:59:59'
          }
          return selectList(Object.assign(parameter, this.queryParam))
            .then(res => {
              this.statistics = res.result.statistics
              return res.result.pageInfo
            })
        },

        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created(){
      selectAll().then(res => {
        this.companys = res.result
      })
    },
    //--############  data() ###############-->
    //--############  methods() ###############-->
    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.transactionId} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      refund(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要退款 ${row.transactionId} 吗?`,
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            refund(row.transactionId).then(res => {
              _this.$message.info(res.message)
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      }
    },
    //--############  methods() ###############-->
    //--############  filters() ###############-->
    filters: {
      statusFilter(state) {
        const statusMap = {
          0: '未支付',
          1: '已支付',
          2: '支付失败',
          3: '已退款'
        }
        return statusMap[state]
      }
    }
    //--############  filters() ###############-->
  }
</script>
